/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MantenimientoLineaDeTransporte.java
 *
 * Created on 28-may-2012, 23:16:20
 */
package Interfaz_Grafica;

import java.awt.Component;
import java.awt.Window;
import java.beans.Statement;
import Logica.LLineaDeTransporte;
import Logica.LVehiculo;
import Objetos.LineaDeTransporte;
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Omar Heráclides
 */
public class MantenimientoLineaDeTransporte extends javax.swing.JFrame {

    private String mensaje;
    private Component parent;
    private Boolean modificable;
    private String CodigoLinea;
    static Statement stmt = null;
    static Connection con = null;
    private int fila = -1;

    public int getFila() {
        return fila;
    }

    public String getCodigoLinea() {
        return CodigoLinea;
    }

    public void setCodigoLinea(String CodigoLinea) {
        this.CodigoLinea = CodigoLinea;
    }

    public void setFila(int fila) {
        this.fila = fila;
    }

    public Boolean getModificable() {
        return modificable;
    }

    public void setModificable(Boolean modificable) {
        this.modificable = modificable;
    }

    public void setParent(Component parent) {
        this.parent = parent;
    }

    public String getMensaje() {
        return mensaje;
    }

    public void setMensaje(String mensaje) {
        this.mensaje = mensaje;
    }

    public MantenimientoLineaDeTransporte() {
        this.setTitle("Mantenimiento de Linea de Transporte");
        initComponents();       
        this.jcbEstadoEmpresa.setVisible(false);
        try {
            CargarEmpresaTransporte();
        } catch (SQLException ex) {
            Logger.getLogger(MantenimientoLineaDeTransporte.class.getName()).log(Level.SEVERE, null, ex);
        }
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAgregarEmpresa = new javax.swing.JButton();
        btnModificarEmpresa = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnLimpiarTabla = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jcbEmpresaTransporte = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtNombreEmpresa = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        dmtLineaTransporte = new javax.swing.JTable();
        btnBuscarEmpresa = new javax.swing.JButton();
        txtBuscarEmpresa = new javax.swing.JTextField();
        jcbBusquedaEmpresa = new javax.swing.JComboBox();
        jcbEstadoEmpresa = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnAgregarEmpresa.setText("Agregar");
        btnAgregarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarEmpresaActionPerformed(evt);
            }
        });

        btnModificarEmpresa.setText("Modificar");
        btnModificarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarEmpresaActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnLimpiarTabla.setText("Limpiar");
        btnLimpiarTabla.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarTablaActionPerformed(evt);
            }
        });

        jLabel1.setText("Nombre: ");

        jLabel2.setText("Empresa de Transporte: ");

        dmtLineaTransporte.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        dmtLineaTransporte.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dmtLineaTransporteMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(dmtLineaTransporte);

        btnBuscarEmpresa.setText("Buscar");
        btnBuscarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarEmpresaActionPerformed(evt);
            }
        });

        txtBuscarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBuscarEmpresaActionPerformed(evt);
            }
        });

        jcbBusquedaEmpresa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nombre", "Estado" }));
        jcbBusquedaEmpresa.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcbBusquedaEmpresaItemStateChanged(evt);
            }
        });

        jcbEstadoEmpresa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Activo", "Inactivo" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnBuscarEmpresa)
                                .addGap(18, 18, 18)
                                .addComponent(txtBuscarEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jcbEstadoEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jcbEmpresaTransporte, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtNombreEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                            .addComponent(jcbBusquedaEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(125, 125, 125)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnLimpiarTabla, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnModificarEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnLimpiar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAgregarEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(29, 29, 29))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAgregarEmpresa)
                    .addComponent(jLabel2)
                    .addComponent(jcbEmpresaTransporte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnModificarEmpresa)
                    .addComponent(jLabel1)
                    .addComponent(txtNombreEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLimpiar)
                        .addGap(56, 56, 56)
                        .addComponent(btnEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnLimpiarTabla))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnBuscarEmpresa)
                            .addComponent(txtBuscarEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jcbEstadoEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jcbBusquedaEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CargarEmpresaTransporte() throws SQLException {
        LVehiculo objLVehiculo = new LVehiculo();
        ArrayList<String> arrl = new ArrayList<String>();
        arrl = objLVehiculo.ObtenerEmpresaTransporte();
        for (int i = 0; i < arrl.size(); i++) {
            this.jcbEmpresaTransporte.addItem(arrl.get(i).toString());
        }
    }
private void txtBuscarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBuscarEmpresaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtBuscarEmpresaActionPerformed

private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
// TODO add your handling code here:
    int resp;
    String mensaje;
    LLineaDeTransporte objLLineaDeTransporte = new LLineaDeTransporte();

    if (modificable == true) {

        resp = JOptionPane.showConfirmDialog(null, "¿Desea Eliminar el registro?", "Pregunta", 0);
        if (resp == 0) {
            int row = this.getFila();
            System.out.println("Columna: " + row + "codigo" + this.getCodigoLinea());
            if (row >= 0) {
                try {
                    //this.setCodVehiculo(Integer.parseInt((String) this.dtmTablaPersonas.getValueAt(row, 0)));
                    mensaje = objLLineaDeTransporte.EliminarVehiculo(this.getCodigoLinea());
                } catch (SQLException ex) {
                    Logger.getLogger(MantenimientoLineaDeTransporte.class.getName()).log(Level.SEVERE, null, ex);
                }

                this.LimpiarTabla();
            }
        }
        this.setModificable(false);
    } else {

        JOptionPane.showMessageDialog(this, "¡Debe seleccionar una fila!");
    }

}//GEN-LAST:event_btnEliminarActionPerformed

private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
// TODO add your handling code here:
    this.txtNombreEmpresa.setText("");
}//GEN-LAST:event_btnLimpiarActionPerformed

private void btnAgregarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarEmpresaActionPerformed
// TODO add your handling code here:
    Boolean pageValid = true;
    int resp;
    pageValid = ValidarPagina();
    resp = JOptionPane.showConfirmDialog(null, "¿Desea Ingresar un nuevo registro?", "Pregunta", 0);
    if (resp == 0) {
        if (pageValid == true) {
            try {
                LineaDeTransporte objLineaDeTransporte = new LineaDeTransporte();
                objLineaDeTransporte.setIdEmpresaTransporte(ObtenerIdEmpresa(this.jcbEmpresaTransporte.getSelectedItem().toString()));
                objLineaDeTransporte.setNombre(this.txtNombreEmpresa.getText());
                objLineaDeTransporte.setEstado("1");
                String numVar = ObtenerIdCorrelativo();
                numVar = numVar.trim();
                System.out.println("NUMERO: " + numVar);
                int numInt = Integer.parseInt(numVar);
                objLineaDeTransporte.setIdLineaTransporte(String.valueOf(numInt + 1));
                LLineaDeTransporte objLLineaTransporte = new LLineaDeTransporte();
                String mensaje = "Fallo";
                mensaje = objLLineaTransporte.AgregarLinea(objLineaDeTransporte);
                JOptionPane.showMessageDialog(null, mensaje, "Información", JOptionPane.INFORMATION_MESSAGE);
            } catch (SQLException ex) {
                Logger.getLogger(MantenimientoLineaDeTransporte.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Debe ingresar caracteres en el campo nombre.",
                    "Información", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}//GEN-LAST:event_btnAgregarEmpresaActionPerformed

private void btnBuscarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarEmpresaActionPerformed

    LLineaDeTransporte objLLLineaDeTransporte = new LLineaDeTransporte();
    if ((this.jcbEstadoEmpresa.isVisible() == false) && (this.txtBuscarEmpresa.getText().equals("") == true)) {
        try {
            this.dmtLineaTransporte.setModel(objLLLineaDeTransporte.MostrarVehiculos());
        } catch (SQLException ex) {
            Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {

        String estado;
        if (this.jcbEstadoEmpresa.getSelectedItem().equals("Activo") == true) {
            estado = "1";
        } else {
            estado = "0";
        }
        try {
            dmtLineaTransporte.setModel(objLLLineaDeTransporte.SelectVehiculo(this.txtBuscarEmpresa.getText(), this.jcbBusquedaEmpresa.getSelectedItem().toString(), estado));
        } catch (SQLException ex) {
            Logger.getLogger(MantenimientoLineaDeTransporte.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}//GEN-LAST:event_btnBuscarEmpresaActionPerformed

private void jcbBusquedaEmpresaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcbBusquedaEmpresaItemStateChanged
    if (this.jcbBusquedaEmpresa.getSelectedItem().equals("Estado")) {
        this.jcbEstadoEmpresa.setVisible(true);
        this.txtBuscarEmpresa.setVisible(false);
    } else {
        this.jcbEstadoEmpresa.setVisible(false);
        this.txtBuscarEmpresa.setVisible(true);
    }
    // TODO add your handling code here:
}//GEN-LAST:event_jcbBusquedaEmpresaItemStateChanged

private void btnLimpiarTablaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarTablaActionPerformed
    LLineaDeTransporte objLLineaDeTransporte = new LLineaDeTransporte();
    this.dmtLineaTransporte.setModel(objLLineaDeTransporte.LimpiarTabla());

}//GEN-LAST:event_btnLimpiarTablaActionPerformed

private void dmtLineaTransporteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dmtLineaTransporteMouseClicked

    int row = this.dmtLineaTransporte.rowAtPoint(evt.getPoint());
    if (row >= 0) {
        this.setModificable(true);
        this.setFila(row);
        //String titulos[] = {"Codigo", "Nombre", "Ap. Paterno", "Ap. Materno", "DNI", "Fecha Nacimiento", "Sexo"};
        this.txtNombreEmpresa.setText(this.dmtLineaTransporte.getModel().getValueAt(row, 2).toString());
        this.jcbEmpresaTransporte.setSelectedItem(row);
        this.setModificable(true);
        this.setCodigoLinea(this.dmtLineaTransporte.getModel().getValueAt(row, 0).toString());
    } else {
        this.setModificable(false);
    }
    // TODO add your handling code here:
}//GEN-LAST:event_dmtLineaTransporteMouseClicked

private void btnModificarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarEmpresaActionPerformed
// TODO add your handling code here:

    int resp;
    LineaDeTransporte objLineaDeTransporte = new LineaDeTransporte();
    System.out.println("Modificable");
    if (modificable == true) {
        System.out.println("Modificable2");
        resp = JOptionPane.showConfirmDialog(null, "¿Desea Modificar el registro?", "Pregunta", 0);
        if (resp == 0) {
            try {
                LimpiarTabla();
                objLineaDeTransporte.setIdLineaTransporte(this.getCodigoLinea());
                objLineaDeTransporte.setNombre(this.txtNombreEmpresa.getText());
                objLineaDeTransporte.setIdEmpresaTransporte(this.ObtenerIdEmpresa(this.jcbEmpresaTransporte.getSelectedItem().toString()));
                this.txtNombreEmpresa.setText("");

                LLineaDeTransporte objLLineaDeTransporte = new LLineaDeTransporte();
                String mensaje = "Fallo";
                mensaje = objLLineaDeTransporte.ModificarVehiculo(objLineaDeTransporte);
                JOptionPane.showMessageDialog(null, mensaje,
                        "Información", JOptionPane.INFORMATION_MESSAGE);
            } catch (SQLException ex) {
                Logger.getLogger(MantenimientoDeVehículos.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.setModificable(false);
        this.txtNombreEmpresa.setText("");
    } else {
        JOptionPane.showMessageDialog(null, "Debe selecionar un registro de la tabla!", "Información", JOptionPane.INFORMATION_MESSAGE);
    }



}//GEN-LAST:event_btnModificarEmpresaActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarEmpresa;
    private javax.swing.JButton btnBuscarEmpresa;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnLimpiarTabla;
    private javax.swing.JButton btnModificarEmpresa;
    private javax.swing.JTable dmtLineaTransporte;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox jcbBusquedaEmpresa;
    private javax.swing.JComboBox jcbEmpresaTransporte;
    private javax.swing.JComboBox jcbEstadoEmpresa;
    private javax.swing.JTextField txtBuscarEmpresa;
    private javax.swing.JTextField txtNombreEmpresa;
    // End of variables declaration//GEN-END:variables

    private void LimpiarTabla() {
        LLineaDeTransporte objLinea = new LLineaDeTransporte();
        this.dmtLineaTransporte.setModel(objLinea.LimpiarTabla());
    }

    private Boolean ValidarPagina() {
        String mensajeValidacion = "";

        if (this.txtNombreEmpresa.getText().equals("") == true) {
            mensajeValidacion = "Ingrese un nombre en el campo nombre";
            this.setMensaje(mensajeValidacion);
            return false;
        }
        return true;
    }

    private String ObtenerIdEmpresa(String nombre) throws SQLException {
        LVehiculo objLVehiculo = new LVehiculo();
        return objLVehiculo.ObtenerIdEmpresaTransporte(nombre);
    }

    private boolean IsLetra(String palabra) {
        for (int i = 0; i < palabra.length(); i++) {
            if (!((palabra.charAt(i) > 64 && palabra.charAt(i) < 91)
                    || (palabra.charAt(i) > 96 && palabra.charAt(i) < 123))) {
                return false;
            }
        }
        return true;
    }

    private String ObtenerIdCorrelativo() throws SQLException {
        LLineaDeTransporte objLLineaDeTransporte;
        objLLineaDeTransporte = new LLineaDeTransporte();
        return objLLineaDeTransporte.ObtenerIdCorrelativo();
    }
}
